import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time
import requests
import base64
import json
import baiduocr as ocr
import login 

# 自动下载并管理 ChromeDriver
url="https://demo.molstone.cn/#/login"
user="admin"
passs="123456"
chrome_options = Options()
chrome_options.add_argument("--start-maximized")  # 最大化窗口
service = Service(executable_path='driver/chromedriver.exe')
    # 创建一个新的Chrome WebDriver实例
driver = webdriver.Chrome(service=service, options=chrome_options)
if(login.login(url,user,passs,driver)==1):
    # 使用 for 循环遍历列表
    # 定义JSON文件的路径
    file_path = 'data/menu.json'
# 打开并读取JSON文件
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    for item in data:
        wait = WebDriverWait(driver, 10)  # 最多等待10秒
        name=item['name']
        element = wait.until(EC.element_to_be_clickable((By.XPATH, f"//*[text()='{name}']")))
        element.click()
        time.sleep(3) 
        for child in item["child"]:
            wait = WebDriverWait(driver, 10)  # 最多等待10秒
            name=child['name'] 
            element = wait.until(EC.element_to_be_clickable((By.XPATH, f"//*[text()='{name}']")))
            element.click()
            time.sleep(3)
            for child2 in child["child"]:
                name=child2['name'] 
                wait = WebDriverWait(driver, 10)  # 最多等待10秒
                element = wait.until(EC.element_to_be_clickable((By.XPATH, f"//*[text()='{name}']")))
                element.click()
                time.sleep(3) 
                



# time.sleep(5) 
# #点击基础配置
# element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='基础配置']")))
# element.click()
# time.sleep(5) 
# element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='基础数据维护']")))
# element.click()
# # 关闭浏览器
# driver.quit()



